import re
from datetime import datetime
from typing import Optional, Any

from ..items import NowcoderItem
# from scrapy.loader import ItemLoader
import scrapy
from env import logger
from lxml import etree


class NowcoderSpider(scrapy.Spider):
    name = "nowcoder"
    allowed_domains = ["ac.nowcoder.com"]
    base_url = "https://ac.nowcoder.com/"

    # def __init__(self, opt=None, *args, **kwargs):
    #     super(NowcoderSpider, self).__init__(*args, **kwargs)
    #     self.opt = opt
    # 好像不加也可以???

    custom_settings = {
        'ITEM_PIPELINES': {'crawl.pipelines.NowcoderPipeline': 400, },
    }

    def start_requests(self):
        match self.opt:  # 别管这个报错，用的命令行传递了参数
            case "race":
                url = self.base_url + "acm/contest/vip-index"
                logger.info(f"url:{url}")
                yield scrapy.Request(url, callback=self.race_parse)  # 指定回调函数,否则默认回调parse函数

    def race_parse(self, response):
        resp = response.text
        resp = etree.HTML(resp)
        resp = resp.xpath("//html/body/div[1]/div[3]/div[1]/div[2]")
        resp = etree.HTML(etree.tostring(resp[0], pretty_print=True).decode())
        resp = resp.xpath("//div[@class='platform-item js-item ']")
        race_dict = []
        for i in resp:
            place = etree.HTML(etree.tostring(i, pretty_print=True).decode())
            race_name = place.xpath("//div[2]/div[1]/h4/a[1]/text()")[0]
            text = place.xpath("//div[2]/div[1]/ul/li/text()")[1]
            start_time_match = re.search(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2})", text)
            end_time_match = re.findall(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2})", text)[1]
            start_time_dt = datetime.strptime(start_time_match.group(0), "%Y-%m-%d %H:%M")
            end_time_dt = datetime.strptime(end_time_match, "%Y-%m-%d %H:%M")
            duration = end_time_dt - start_time_dt
            start_timestamp = start_time_dt.timestamp()
            end_timestamp = end_time_dt.timestamp()
            seconds = int(end_timestamp - start_timestamp)
            hours, remainder = divmod(seconds, 3600)
            minutes, _ = divmod(remainder, 60)
            formatted_time = f"{hours:02}:{minutes:02}"
            race_dict.append([int(start_timestamp), race_name, formatted_time])
        item = NowcoderItem()
        item['race_dict'] = race_dict
        yield item
